package com.metronoteapp;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.metronoteapp.database.GroupNoteDao;
import com.metronoteapp.database.NoteDao;
import com.metronoteapp.model.GroupModel;
import com.metronoteapp.model.NoteModel;
import com.metronoteapp.until.Constants;

public class AddNoteActivity extends Activity implements OnClickListener {
	private String TAG = AddNoteActivity.class.getSimpleName();
	private String mNoteId;
	private String mFlgImp;
	private Context mContext;
	private Bundle mBundle;
	private ImageView btnSaveNote;
	private ImageView btnCancelNote;
	private TextView txtTitle;
	private TextView txtContent;
	private String txtGroup;
	private NoteModel mObjNoteModel;
	private NoteDao mObjNoteDao;
	private GroupNoteDao mObjGroupDao;
	private GroupModel mObjGroupModel;
	private List<String> choices;
	private Spinner spnGroupNote;
	private Boolean mBlnUpdate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_add_new_note);

		// Set full Screen activity
		WindowManager.LayoutParams attrs = this.getWindow().getAttributes();
		attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
		this.getWindow().setAttributes(attrs);
		this.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		mContext = this;
		mBlnUpdate = false;

		setGroupDataForSpinner();
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				AddNoteActivity.this,
				android.R.layout.simple_dropdown_item_1line, choices);
		spnGroupNote = (Spinner) findViewById(R.id.spinner1);
		spnGroupNote.setAdapter(adapter);

		mBundle = getIntent().getExtras();
		mNoteId = mBundle.getString("NoteId");
		mFlgImp = mBundle.getString("FlgImp");
		Log.v(TAG, "mNoteId: " + mNoteId);
		txtTitle = (TextView) findViewById(R.id.txtAddTitelNote);
		txtContent = (TextView) findViewById(R.id.txtAddDetailNote);
		
		//Chek update or insert 
		if(mNoteId.equalsIgnoreCase("")){
			mBlnUpdate = false;
		}else{
			mBlnUpdate = true;
		}
		
		//Get infor note
		if(mBlnUpdate){
			mObjNoteDao = new NoteDao(mContext);
			mObjNoteModel = new NoteModel();
			mObjNoteModel.setId(Integer.parseInt(mNoteId));
			mObjNoteDao.openDB();
			Cursor mCursor = mObjNoteDao.getNote(mObjNoteModel);
			Log.v(TAG, "Note update : " + mCursor.getCount());
			if (mCursor.moveToFirst()) {
				do {
					mObjNoteModel = new NoteModel();
					mObjNoteModel.setId(Integer.parseInt(getColumnAsString(mCursor,
							0)));
					mObjNoteModel.setTitle(getColumnAsString(mCursor, 1));
					mObjNoteModel.setContent(getColumnAsString(mCursor, 2));
					mObjNoteModel.setNoteType(getColumnAsString(mCursor, 3));
					mObjNoteModel.setDateTime(getColumnAsString(mCursor, 4));
					mObjNoteModel.setGroupId(Integer.parseInt(getColumnAsString(
							mCursor, 5)));
					mObjNoteModel.setFlgFav(Integer.parseInt(getColumnAsString(
							mCursor, 6)));
				} while (mCursor.moveToNext());
			}
			mObjNoteDao.closeDB();
			txtTitle.setText(mObjNoteModel.getTitle());
			txtContent.setText(mObjNoteModel.getContent());
			
			mObjGroupDao = new GroupNoteDao(mContext);
			mObjGroupDao.openDB();
			mObjGroupModel = new GroupModel();
			mObjGroupModel.setGroupId(mObjNoteModel.getGroupId());
			mCursor = mObjGroupDao.getGroup(mObjGroupModel);
			if (mCursor.moveToFirst()) {
				do {
					if(choices != null){
						if(choices.size() >= 1){
							Log.v(TAG, "Position of note group: " + choices.indexOf(getColumnAsString(mCursor,1)));
							spnGroupNote.setSelection(choices.indexOf(getColumnAsString(mCursor,1)));
						}
					}
				} while (mCursor.moveToNext());
			}
			mObjGroupDao.closeDB();
		}
		btnCancelNote = (ImageView) findViewById(R.id.tooltip_confirm);
		btnSaveNote = (ImageView) findViewById(R.id.tooltip_cancel);
		btnCancelNote.setOnClickListener(this);
		btnSaveNote.setOnClickListener(this);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			onBackPressed();
			return true;
		}
		return super.onKeyUp(keyCode, event);
	}

	@Override
	public void onBackPressed() {
		Intent callMainNote = new Intent(mContext, MainActivity.class);
		mContext.startActivity(callMainNote);
		return;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tooltip_confirm:
			if(txtTitle.getText().toString().trim().equalsIgnoreCase("")){
				Toast.makeText(mContext, "Title of note not accept blank value!", Toast.LENGTH_LONG).show();
			}else {
				saveNewNote();
			}
			break;
		case R.id.tooltip_cancel:
			Intent callMainNote = new Intent(mContext, MainActivity.class);
			mContext.startActivity(callMainNote);
			break;
		default:
			break;
		}
	}
	
	/*
	 * This function parse cursor
	 */
	private static String getColumnAsString(Cursor cursor, int index) {
		return cursor.getString(index).trim();
	}

	/*
	 * Function save new note
	 * */
	public void saveNewNote() {
		int intGroupId;
		txtGroup = (String) spnGroupNote.getItemAtPosition(spnGroupNote.getSelectedItemPosition());
		mObjGroupDao = new GroupNoteDao(mContext);
		mObjGroupDao.openDB();
		mObjGroupModel = new GroupModel();
		mObjGroupModel.setGroupName(txtGroup);
		Cursor mCursor = mObjGroupDao.getGroupByName(mObjGroupModel);
		if (mCursor.moveToFirst()) {
			do {
				intGroupId = Integer.parseInt(getColumnAsString(mCursor,0));
			} while (mCursor.moveToNext());
		}else{
			intGroupId = 0;
		}
		Log.v(TAG, "saveNewNote() | GroupID add : " + intGroupId);
		mObjGroupDao.closeDB();

		//Update or insert note
		Date cDate = new Date();
		String fDate = new SimpleDateFormat("yyyy-MM-dd").format(cDate);
		
		mObjNoteDao = new NoteDao(mContext);
		mObjNoteDao.openDB();
		mObjNoteModel = new NoteModel();
		mObjNoteModel.setTitle(txtTitle.getText().toString().trim());
		mObjNoteModel.setContent(txtContent.getText().toString().trim());
		mObjNoteModel.setGroupId(intGroupId);
		mObjNoteModel.setFlgFav(Integer.parseInt(mFlgImp));
		mObjNoteModel.setNoteType(Constants.TYPE_TEXT);
		mObjNoteModel.setDateTime(fDate);
		if(mBlnUpdate){
			mObjNoteModel.setId(Integer.parseInt(mNoteId));
			mObjNoteDao.updateNote(mObjNoteModel);
		}else{
			mObjNoteDao.insertNote(mObjNoteModel);
		}
		mObjNoteDao.closeDB();
		Log.v(TAG, "saveNewNote() | Added : " + mObjNoteModel.getTitle());
		Intent callMainNote = new Intent(mContext, MainActivity.class);
		mContext.startActivity(callMainNote);
	}
	
	/*
	 * Setting data for spinner
	 * */
	public void setGroupDataForSpinner() {
		mObjGroupDao = new GroupNoteDao(mContext);
		choices = new ArrayList<String>();
		mObjGroupDao.openDB();
		Cursor mCursor = mObjGroupDao.getAllRecord();
		Log.v(TAG, "setGroupDataForSpinner() | " + mCursor.getCount());
		if (mCursor.moveToFirst()) {
			do {
				Log.v(TAG, "setGroupDataForSpinner() | Valued: " + getColumnAsString(mCursor, 1));
				choices.add(getColumnAsString(mCursor, 1));
			} while (mCursor.moveToNext());
		}
		mObjGroupDao.closeDB();
	}
}
